package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.bo.HcxAppVersionAddBo;
import com.ruoyi.business.bo.HcxAppVersionQueryBo;
import com.ruoyi.business.bo.HcxAppVersionEditBo;
import com.ruoyi.business.domain.HcxAppVersion;
import com.ruoyi.business.mapper.HcxAppVersionMapper;
import com.ruoyi.business.vo.HcxAppVersionVo;
import com.ruoyi.business.service.IHcxAppVersionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * app版本管理Service业务层处理
 *
 * @author wsdz
 * @date 2022-09-02
 */
@Service
public class HcxAppVersionServiceImpl extends ServicePlusImpl<HcxAppVersionMapper, HcxAppVersion> implements IHcxAppVersionService {

    @Override
    public HcxAppVersionVo queryById(Long id){
        return getVoById(id, HcxAppVersionVo.class);
    }

    @Override
    public TableDataInfo<HcxAppVersionVo> queryPageList(HcxAppVersionQueryBo bo) {
        PagePlus<HcxAppVersion, HcxAppVersionVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HcxAppVersionVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<HcxAppVersionVo> queryList(HcxAppVersionQueryBo bo) {
        return listVo(buildQueryWrapper(bo), HcxAppVersionVo.class);
    }

    private LambdaQueryWrapper<HcxAppVersion> buildQueryWrapper(HcxAppVersionQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HcxAppVersion> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getAppName()), HcxAppVersion::getAppName, bo.getAppName());
        lqw.eq(StrUtil.isNotBlank(bo.getAppVersion()), HcxAppVersion::getAppVersion, bo.getAppVersion());
        lqw.eq(StrUtil.isNotBlank(bo.getAppType()), HcxAppVersion::getAppType, bo.getAppType());
        lqw.eq(StrUtil.isNotBlank(bo.getFileUrl()), HcxAppVersion::getFileUrl, bo.getFileUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getDelFlag()), HcxAppVersion::getDelFlag, bo.getDelFlag());
        lqw.orderByDesc(HcxAppVersion::getAppVersion);
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(HcxAppVersionAddBo bo) {
        HcxAppVersion add = BeanUtil.toBean(bo, HcxAppVersion.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(HcxAppVersionEditBo bo) {
        HcxAppVersion update = BeanUtil.toBean(bo, HcxAppVersion.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HcxAppVersion entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importHcxAppVersion(List<HcxAppVersionVo> hcxAppVersionList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(hcxAppVersionList), "导入数据不能为空");
        int successNum = 0;
        for (HcxAppVersionVo hcxAppVersion : hcxAppVersionList) {
            HcxAppVersion add = BeanUtil.toBean(hcxAppVersion, HcxAppVersion.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", hcxAppVersionList.size(), successNum);
        if (successNum != hcxAppVersionList.size()) {
            msg = msg + String.format("失败 %d 条！", hcxAppVersionList.size() - successNum);
        }
        return msg;
    }
}
